package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Follow;
import cn.jinjimi.app.entity.PictureShow;
import cn.jinjimi.app.managers.OpenActivityManager;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by libilang on 15/11/6.
 */
public class CustomArchivesPhotosAdapter extends MyBaseAdapter<Follow.Attachment> {
    public Context mContext;
    List<String> bigUrls = new ArrayList<String>();
    List<String> smallUrls = new ArrayList<String>();

    public CustomArchivesPhotosAdapter(Context context) {
        mContext = context;
    }

    @Override
    public void setItems(List<Follow.Attachment> items) {
        super.setItems(items);
        smallUrls.clear();
        bigUrls.clear();
        for (Follow.Attachment attachments : getItems()) {
            smallUrls.add(attachments.thumbnailUrl);
            bigUrls.add(attachments.imageUrl);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final FollowPhotoHolder holder;
        Follow.Attachment attachment = getItem(position);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_follow_photo, null);
            holder = new FollowPhotoHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.img_follow);
            convertView.setTag(holder);
        } else {
            holder = (FollowPhotoHolder) convertView.getTag();
        }
        if (attachment != null) {
            holder.image.setImageURI(Uri.parse(attachment.thumbnailUrl));
            holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<PictureShow> pictureShows = PictureShow.createPhotoNetShows(mContext, smallUrls, bigUrls, holder.image);
                    OpenActivityManager.getInstance().startPhotoNormalBrowserActivity(mContext, pictureShows, position);
                }
            });
        }

        return convertView;
    }

    public class FollowPhotoHolder {
        public ImageView image;
    }
}


